package com.jaws.demo.service;

import com.google.common.collect.Lists;
import com.jaws.core.common.util.DateUtil;
import com.jaws.demo.common.enums.TableSequenceEnum;
import com.jaws.demo.domain.AccountTrade;
import com.jaws.demo.dao.AccountTradeDao;
import com.jaws.demo.common.sharding.util.CommonIdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 账户交易服务接口实现
 *
 * @author liangqm
 * @date 2018-05-03 10:24:18
 * @since v1.0.0
 */
@Service
public class AccountTradeServiceImpl implements AccountTradeService {

	@Resource
	private
	AccountTradeDao accountTradeDao;

	@Resource
	protected CommonIdUtils commonIdUtils;

	@Override
	public void trade(AccountTrade accountTrade) {
		accountTradeDao.insert(accountTrade);
	}

	@Override
	public void batchTrade(List<AccountTrade> accountTradeList) {

		int size = 80000000;

		List<AccountTrade> accountTrades = Lists.newArrayList();

		for (int i = 0; i < size; i++) {
			AccountTrade accountTrade = new AccountTrade();
			accountTrades.add(accountTrade);

			accountTrade.setTradeNo(commonIdUtils.generateId(TableSequenceEnum.ACCOUNT_ORDER));
			accountTrade.setOutTradeNo("out_trade_no_" + i);
			accountTrade.setOutBusinessNo("out_B_no" + i);
			accountTrade.setUserId("user_id" + i);
			accountTrade.setAccountNo("ac_no" + i);
			accountTrade.setAmount(Long.valueOf(i));
			accountTrade.setTradeTime(DateUtil.getCurrentDate());
			accountTrade.setTradeType("01");
			accountTrade.setCreateTime(DateUtil.getCurrentDateTime());
			accountTrade.setUpdateTime(DateUtil.getCurrentDateTime());
			accountTrade.setSource("1");
			accountTrade.setRemark("备注" + i);

//			accountTradeDao.batchInsert(accountTrades);

			// 批量插入 1000条
//			if (accountTrades.size() >= 10) {
			accountTradeDao.batchInsert(accountTrades);
			accountTrades = Lists.newArrayList();
//			}
		}
	}
}
